package com.yhao.stock.controller;


import com.yhao.stock.common.service.SHService;
import com.yhao.stock.common.utils.DateUtil;
import com.yhao.stock.common.utils.FileUtil;
import com.yhao.stock.entity.StockInfo;
import com.yhao.stock.service.StockInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 历史交易数据 前端控制器
 * </p>
 *
 * @author yhao
 * @since 2023-11-09
 */
@RestController
@RequestMapping("/hisData")
public class HisDataController {

    @Autowired
    private SHService shService;

    @Autowired
    private StockInfoService stockInfoService;

    @GetMapping("/getAllHisInfos")
    public String getAllHisInfos() {
        final List<StockInfo> list = stockInfoService.list();
        String code = "";
        String name = "";
        String startDate = "20231113";
        String endDate = DateUtil.getDate("yyyyMMdd");
        // String endDate = "20231113";
        for (StockInfo stockInfo : list) {
            code = stockInfo.getdCode();
            name = stockInfo.getdName();
            shService.getHisInfo(code, name, startDate, endDate);
        }
        return "success";
    }

    @GetMapping("/getRangeHisInfos/{startId}/{endId}")
    public String getRangeHisInfos(@PathVariable Integer startId, @PathVariable Integer endId) {
        final List<StockInfo> list = stockInfoService.listByRange(startId, endId);
        String code = "";
        String name = "";
        String startDate = "20100101";
        // String endDate = DateUtil.getDate("yyyyMMdd");
        String endDate = "20231110";
        for (StockInfo stockInfo : list) {
            code = stockInfo.getdCode();
            name = stockInfo.getdName();
            shService.getHisInfo(code, name, startDate, endDate);
        }
        return "success";
    }

    @GetMapping("/getIdsInfo")
    public String getIdsInfo() {
        final List<String> allList = FileUtil.readTxt("D:\\a.txt");
        final List<String> existList = FileUtil.readTxt("D:\\b.txt");
        for (String dCode : allList) {
            if (!existList.contains(dCode)) {
                String dName = stockInfoService.getNameByCode(dCode);
                String startDate = "20100101";
                String endDate = "20231110";
                shService.getHisInfo(dCode, dName, startDate, endDate);
            }
        }
        return "success";
    }

}
